package com.e3.basdoc.employee.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.e3.basdoc.employee.dao.EmployeeDAO;
import com.e3.basdoc.employee.entity.EmployeeEntity;
import com.e3.basdoc.employee.model.EmployeeModel;
import com.e3.basdoc.employee.model.EmployeeQC;
import com.e3.basdoc.employee.service.EmployeeCacheManager;
import com.e3.basdoc.employee.service.EmployeeQueryService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperQueryService;

public class EmployeeQueryServiceImpl extends SuperQueryService implements EmployeeQueryService{
	
	private EmployeeDAO employeeDAO;
	
	private EmployeeDAO getEmployeeDAO(){
		if(employeeDAO == null){
			employeeDAO = DAOHelper.createDAOInstance(EmployeeDAO.class);
		}
		return employeeDAO;
	}

	public List<EmployeeModel> getEmployeeByCondition(EmployeeQC qc) throws ServiceException {
		
		List<Long> ids = getEmployeeDAO().getEmployeeIdsByCondition(qc);
		if(ids == null||ids.isEmpty()){
			return null;
		}
		
		List<EmployeeModel> models = new ArrayList<EmployeeModel>(ids.size());
		for (Long id : ids) {
			EmployeeModel model = getEmployeeById(id);
			models.add(model);
		}
		return models;
	}
	/**
	 * the method is used to load data into cache.
	 */
	public List<EmployeeModel> getAllEmployees() throws ServiceException {
		List<EmployeeEntity> entities = getEmployeeDAO().getAllEmployee();
		if(entities==null||entities.isEmpty()){
			return null;
		}
		List<EmployeeModel> models = new ArrayList<EmployeeModel>(entities.size());
		for (EmployeeEntity employeeEntity : entities) {
			models.add(employeeEntityToModel(employeeEntity));
		}
		return models;
	}

	public EmployeeModel getEmployeeById(long id) throws ServiceException {
		EmployeeModel model = EmployeeCacheManager.INSTANCE.get(id);
		if(model==null){
			EmployeeEntity entity = getEmployeeDAO().getById(EmployeeEntity.class, id);
			model = employeeEntityToModel(entity);
			if(model != null){
				EmployeeCacheManager.INSTANCE.put(model);
			}
		}
		
		return model;
	}

	public List<EmployeeModel> getEmployeeByIds(List<Long> ids)	throws ServiceException {
		if(ids == null||ids.isEmpty()){
			return null;
		}
		
		List<EmployeeModel> models = new ArrayList<EmployeeModel>(ids.size());
		for (Long id : ids) {
			EmployeeModel model = getEmployeeById(id);
			models.add(model);
		}
		
		return models;
	}

	private EmployeeModel employeeEntityToModel(EmployeeEntity entity){
		
		if(entity == null){
			return null;
		}
		
		EmployeeModel model = new EmployeeModel();
		model = ocEntityToModel(model, entity);
		
		model.setCode(entity.getCode());
		model.setName(entity.getName());
		model.setBankAccount(entity.getBankAccount());
		model.setBankId(entity.getBankId());
		Long date = entity.getBirthday();
		if(date!=null&&date>0){
			model.setBirthday(new Date(date));
		}
		model.setClassesId(entity.getClassesId());
		model.setDeptId(entity.getDeptId());
		model.setEducation(entity.getEducation());
		model.setEmail(entity.getEmail());
		model.setFormerName(entity.getFormerName());
		model.setGender(entity.getGender());
		model.setHomeAddress(entity.getHomeAddress());
		model.setHomePhone(entity.getHomePhone());
		model.setHomePostcode(entity.getHomePostcode());
		model.setIdcardNo(entity.getIdcardNo());
		model.setMobile(entity.getMobile());
		model.setOfficePhone(entity.getOfficePhone());
		date = entity.getOnboardDate();
		if(date != null&&date>0){
			model.setOnboardDate(new Date(date));
		}
		date = entity.getQuitDate();
		if(date != null&&date>0){
			model.setQuitDate(new Date(date));
		}
		
		model.setRank(entity.getRank());
		model.setRemark(entity.getRemark());
		model.setSkillLevel(entity.getSkillLevel());
		model.setStatus(entity.getStatus());
		date = entity.getWorkFromDate();
		if(date != null&&date>0){
			model.setWorkFromDate(new Date(date));
		}
		return model;
	}

	public List<EmployeeModel> getPagingEmployee(EmployeeQC qc, int start, int max)	throws ServiceException {
		List<Long> ids = getEmployeeDAO().getPagingEmployeeIdsByCondition(qc,start,max);
		if(ids!=null&&ids.size()>0){
			return getEmployeeByIds(ids);
		}
		return null;
	}

	public Long getEmployeeSizeByCondition(EmployeeQC qc) throws ServiceException {
		return getEmployeeDAO().counterByCondition(qc);
	}
}
